<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Model;
use app\common\model\Cart as CartModel;

class Cart extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];


    /** 列表 */
    function list()
    {
        $params = $this->request->param();
        $this->validateCommon('Cart.list', $this->serial_number);
        $list = CartModel::where(['user_id'=>$this->auth->id])
                 ->order('create_time desc')
                 ->field('id,user_id userId,product_id productId,product_main_image productImage'
                 .',product_name productName,product_num productNum,product_total_price productTotalPrice'
                 )->paginate($params['pageCount'],false,['page'=>$params['page']]);
        $this->success('购物车列表',$list,1,$this->serial_number);
    }

    /** 添加购物车 */
    function add()
    {
        $params = $this->request->param();
        $this->validateCommon('Cart.add', $this->serial_number);
        $beforeLog   = CartModel::where(['user_id' => $this->auth->id, 'product_id' => $params['productId']])->find();
        $productInfo = \app\common\model\Product::get($params['productId']);
        if ($beforeLog) {
            $newProductNum   = bcadd($beforeLog['product_num'], $params['productNum']);
            $newProductPrice = bcmul($newProductNum, $productInfo->product_price);
            $updateArr       = [
                'user_id'             => $this->auth->id,
                'product_id'          => $params['productId'],
                'product_image'       => $beforeLog['product_image'],
                'product_name'        => $beforeLog['product_name'],
                'product_num'         => $newProductNum,
                'product_total_price' => $newProductPrice,
            ];
          $res =   CartModel::where('id',$beforeLog['id'])->update($updateArr);
        }else{
            $newProductPrice = bcmul($params['productNum'], $productInfo->product_price);
            $updateArr       = [
                'user_id'             => $this->auth->id,
                'product_id'          => $params['productId'],
                'product_image'       => $productInfo->product_main_image,
                'product_name'        => $productInfo->product_name,
                'product_num'         => $params['productNum'],
                'product_total_price' => $newProductPrice,
            ];

          $res =  CartModel::create($updateArr);
        }
        if($res) $this->success('添加购物车成功','',1,$this->serial_number);
        $this->error('添加购物车失败','',0,$this->serial_number);
    }


    /** 修改 */
    function update()
    {
        $params = $this->request->param();
        $this->validateCommon('Cart.update', $this->serial_number);
        $beforeLog   = CartModel::where(['user_id' => $this->auth->id, 'product_id' => $params['productId']])->find();
        $productInfo = \app\common\model\Product::get($params['productId']);
        $res = [];
        if ($beforeLog) {
            $newProductNum   = bcadd($beforeLog['product_num'], $params['productNum']);
            $newProductPrice = bcmul($newProductNum, $productInfo->product_price);
            $updateArr       = [
                'user_id'             => $this->auth->id,
                'product_id'          => $params['productId'],
                'product_image'       => $beforeLog['product_image'],
                'product_name'        => $beforeLog['product_name'],
                'product_num'         => $newProductNum,
                'product_total_price' => $newProductPrice,
            ];
            $res =   CartModel::where('id',$beforeLog['id'])->update($updateArr);
        }else{
            $this->error('该商品信息获取失败','',0,$this->serial_number);
        }
        $this->success('修改成功',$res,1,$this->serial_number);
    }

    /** 删除 */
    function del()
    {
        $params = $this->request->param();
        $this->validateCommon('Cart.del', $this->serial_number);
        $beforeLog   = CartModel::where(['user_id' => $this->auth->id, 'product_id' => $params['productId']])->find();
        if(!$beforeLog) $this->error('该商品信息获取失败','',0,$this->serial_number);
        $res = CartModel::destroy(['user_id' => $this->auth->id, 'product_id' => $params['productId']]);
        $this->success('删除成功','',1,$this->serial_number);
    }
}